package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.staservice.entity.StatisticsCourseinfo;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsCourseinfoMapper;
import com.atguigu.staservice.service.StatisticsCourseinfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author gengky
 * @since 2022-05-07
 */
@Service
public class StatisticsCourseinfoServiceImpl extends ServiceImpl<StatisticsCourseinfoMapper, StatisticsCourseinfo> implements StatisticsCourseinfoService {

    @Override
    public Map<String, Object> getShowCourseData(String courseTitle,String type, String begin, String end) {
        //根据条件查询对应数据
        QueryWrapper<StatisticsCourseinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_title",courseTitle);
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsCourseinfo> staList = baseMapper.selectList(wrapper);
        //因为返回有两部分数据：日期 和 日期对应数量
        //前端要求数组json结构，对应后端java代码是list集合
        //创建两个list集合，一个日期list，一个数量list
        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        //遍历查询所有数据list集合，进行封装
        for (int i = 0; i < staList.size(); i++) {
            StatisticsCourseinfo daily = staList.get(i);
            //封装日期list集合
            date_calculatedList.add(daily.getDateCalculated());
            //封装对应数量
            switch (type) {
                case "course_point":
                    numDataList.add(daily.getCoursePoint());
                    break;
                case "course_buy":
                    numDataList.add(daily.getCourseBuy());
                    break;
                default:
                    break;
            }
        }
        //把封装之后两个list集合放到map集合，进行返回
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("numDataList",numDataList);
        return map;
    }


    @Override
    public void setCourseCount(String title,String day) {
        //添加记录之前删除表相同日期的数据
        QueryWrapper<StatisticsCourseinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        //把获取数据添加数据库，统计分析表里面
        StatisticsCourseinfo courseinfo=new StatisticsCourseinfo();
        //TODO:根据课程Title查询课程ID
        courseinfo.setCourseId(null);
        courseinfo.setCourseTitle(title);
        //
        courseinfo.setDateCalculated(day);
        //
        int anInt = RandomUtils.nextInt(10, 90);
        courseinfo.setCourseBuy(anInt);
        courseinfo.setCoursePoint(anInt+RandomUtils.nextInt(50,90));

        baseMapper.insert(courseinfo);
    }
}
